package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.FoodOrder;
import com.tourism.mapper.FoodOrderMapper;
import com.tourism.service.FoodOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FoodOrderServiceImpl extends ServiceImpl<FoodOrderMapper, FoodOrder> implements FoodOrderService {

    @Override
    public Result getOrderById(Integer id) {
        if (id == null) {
            return Result.fail("订单ID不能为空");
        }
        FoodOrder order = getById(id);
        return Result.success(order);
    }

    @Override
    public Result getOrderList(Integer page, Integer pageSize, String userName, String foodName) {
        // 创建分页对象
        Page<FoodOrder> pageParam = new Page<>(page, pageSize);
        // 创建 LambdaQueryWrapper 用于构建查询条件
        LambdaQueryWrapper<FoodOrder> wrapper = new LambdaQueryWrapper<>();

        // 添加模糊查询条件
        if (userName != null && !userName.isEmpty()) {
            wrapper.like(FoodOrder::getUserName, userName);
        }
        if (foodName != null && !foodName.isEmpty()) {
            wrapper.like(FoodOrder::getFoodName, foodName);
        }

        // 执行分页查询
        IPage<FoodOrder> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }

    @Override
    public Result addOrder(FoodOrder foodOrder) {
        // 校验用户名称
        if (foodOrder.getUserName() == null || foodOrder.getUserName().isEmpty()) {
            return Result.fail("用户名称不能为空");
        }
        // 校验食物名称
        if (foodOrder.getFoodName() == null || foodOrder.getFoodName().isEmpty()) {
            return Result.fail("食物名称不能为空");
        }
        // 校验数量
        if (foodOrder.getQuantity() == null || foodOrder.getQuantity() <= 0) {
            return Result.fail("数量必须大于0");
        }
        // 校验总价
        if (foodOrder.getTotalPrice() == null || foodOrder.getTotalPrice().compareTo(java.math.BigDecimal.ZERO) <= 0 ) {
            return Result.fail("总价必须大于0");
        }

        // 保存订单信息
        boolean success = save(foodOrder);
        return Result.success(success);
    }

    @Override
    public Result updateOrder(FoodOrder foodOrder) {
        // 校验订单ID
        if (foodOrder.getId() == null) {
            return Result.fail("订单ID不能为空");
        }

        // 更新订单信息
        boolean success = updateById(foodOrder);
        return Result.success(success);
    }

    @Override
    public Result deleteOrder(Integer id) {
        // 校验订单ID
        if (id == null) {
            return Result.fail("订单ID不能为空");
        }

        // 删除订单信息
        boolean success = removeById(id);
        return Result.success(success);
    }

    @Override
    @Transactional
    public Result batchDeleteOrders(List<Integer> ids) {
        // 校验ID列表
        if (ids == null || ids.isEmpty()) {
            return Result.fail("订单ID列表不能为空");
        }

        // 批量删除订单信息
        boolean success = removeBatchByIds(ids);
        return Result.success(success);
    }

    @Override
    public Result getAllOrders() {
        // 获取所有订单信息
        List<FoodOrder> orders = list();
        return Result.success(orders);
    }
}
